import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-07-24
 * Time: 18:41
 */
public class test1 {
    public int[][] merge(int[][] intervals) {
        int n = intervals.length;
        if(n==0){
            return new int[0][0];
        }
        // 根据二维数组中的子数组的第一个元素[0]进行排序
        Arrays.sort(intervals,(val1, val2) -> val1[0]-val2[0]);
        List<int[]> ret = new ArrayList<>();
        ret.add(intervals[0]);
        for(int i=0;i<n;i++){
            int l = intervals[i][0];
            int r = intervals[i][1];
            if(l<=ret.get(ret.size()-1)[1]){
                ret.get(ret.size()-1)[1] = Math.max(r,ret.get(ret.size()-1)[1]);
            }else{
                ret.add(intervals[i]);
            }
        }
        return ret.toArray(new int[ret.size()-1][]);
    }
}
